package com.cc.spring.controller;

import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.cc.spring.common.CommonResp;
import com.cc.spring.exception.BusinessException;
import com.cc.spring.exception.BusinessExceptionCode;
import com.cc.spring.request.DiskUserRequest;
import com.cc.spring.request.UserLikeRequest;
import com.cc.spring.request.UserPwdRequest;
import com.cc.spring.resp.UserInfoResp;
import com.cc.spring.resp.UserResp;
import com.cc.spring.service.UserService;
import com.cc.spring.utils.AuthUtil;
import com.cc.spring.utils.JWTUtil;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
// 用户控制器
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
//    添加用户
    @PostMapping("/add")
    public CommonResp addUser(@Valid @RequestBody DiskUserRequest diskUserRequest){
//        这个接口用于处理添加用户和更新用户
        CommonResp commonResp = new CommonResp();
//        如果传递过来的值没有id，那么表示新增，否则表示更新
        if(!ObjectUtils.isEmpty(diskUserRequest.getId())){
            userService.updateUserInfo(diskUserRequest);
            commonResp.setSuccess(true);
            commonResp.setMessage("更新用户信息成功");
            return commonResp;
        }
        commonResp.setSuccess(true);
        commonResp.setMessage("新增用户成功");
        userService.addUser(diskUserRequest);
        return commonResp;
    }

//    登录
    @PostMapping("/login")
    public CommonResp<UserResp> login(@Valid @RequestBody DiskUserRequest diskUserRequest){
        CommonResp<UserResp> commonResp = new CommonResp();
        UserResp userResp = userService.userLogin(diskUserRequest);
        commonResp.setContent(userResp);
        return commonResp;
    }

//    获取用户的信息，这个接口用于管理员
    @GetMapping("/list")
    public CommonResp<List<UserInfoResp>> getAllUserList(HttpServletRequest request, UserLikeRequest userLikeRequest){
        CommonResp<List<UserInfoResp>> userList = new CommonResp<>();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.decodeToken(token);
        String permission1 = decodedJWT.getClaim("permission").asString();
//        == 0 那么就表示超级管理员，其他为普通用户
        if(!permission1.equals("0")){
//            返回一个空对象
            userList.setSuccess(false);
            userList.setMessage("获取用户失败");
            return userList;
        }

        List<UserInfoResp> allUser = userService.getAllUser(userLikeRequest);
        userList.setContent(allUser);
        userList.setSuccess(true);
        userList.setMessage("获取成功");
        return userList;

    }
//    删除一个用户
    @DeleteMapping("/del")
    public CommonResp delUser(Long id, HttpServletRequest request){
        if(AuthUtil.authIsAble(request)) throw new BusinessException(BusinessExceptionCode.PERMISSION_NOT_ABLE);
        CommonResp commonResp = new CommonResp();
        System.out.println(id);
        userService.deleteUserByUserId(id);
        commonResp.setMessage("删除成功");
        return commonResp;
    }
//    修改密码
    @PutMapping("/rePwd")
    public CommonResp updateUserPassword(@Valid @RequestBody UserPwdRequest userPwdRequest, HttpServletRequest request){
//       没有权限抛异常
        if(AuthUtil.authIsAble(request)) throw new BusinessException(BusinessExceptionCode.PERMISSION_NOT_ABLE);
        CommonResp commonResp = new CommonResp();
        userService.updateUserPassword(userPwdRequest);
        commonResp.setMessage("修改成功");
        return commonResp;
    }
}
